package com.hfjyz.base.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hfjyz.base.bean.BaseBean;
import com.hfjyz.base.bean.SearchCondition;
import com.hfjyz.base.exception.BaseException;
import com.opensymphony.xwork2.ActionSupport;

/**
 * Grid base action class.
 *
 */
public abstract class GridBaseAction extends ActionSupport {
    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = -6669308518186365377L;

    /** result json key:page -> current page number */
    protected static final String PAGE_NUM = "page";

    /** total page count */
    protected static final String TOTAL_PAGE_COUNT = "total";

    /** records count */
    protected static final String RECORDS_COUNT = "records";

    protected static final String ROWS_DATA = "rows";
    
    protected static final String CELL_ID = "id";
    
    protected static final String CELL = "cell";

    /**
     * 結果JSON map
     */
    protected Map<String, Object> map = new HashMap<String, Object>();

    /**
     * page number
     */
    protected int pageNum = 0;

    /**
     * rows' count per page
     */
    protected int rowCountPerPage = 0;

    /**
     * sort col
     */
    protected String sortColumn = null;

    /**
     * sort direction
     */
    protected String sortDirection = null;

    public Map<String, Object> getMap() {
        return map;
    }

    public void setMap(Map<String, Object> map) {
        this.map = map;
    }

    public int getPage() {
        return pageNum;
    }

    public void setPage(int page) {
        this.pageNum = page;
    }

    public int getRows() {
        return rowCountPerPage;
    }

    public void setRows(int rows) {
        this.rowCountPerPage = rows;
    }

    public String getSidx() {
        return sortColumn;
    }

    public void setSidx(String sidx) {
        this.sortColumn = sidx;
    }

    public String getSord() {
        return sortDirection;
    }

    public void setSord(String sord) {
        this.sortDirection = sord;
    }

    public String getList() {
        map = new HashMap<String, Object>();
        try {
            long count = getTotalCount();
            map.put(RECORDS_COUNT, count);
            map.put(PAGE_NUM, pageNum);

            long totalPageCount = 0;
            if (count % rowCountPerPage != 0) {
                totalPageCount = count / rowCountPerPage + 1;
            } else {
                totalPageCount = count / rowCountPerPage;
            }
            map.put(TOTAL_PAGE_COUNT, totalPageCount);

            int from = (pageNum - 1) * rowCountPerPage;
            List<? extends BaseBean> beanList = getBeanList();
            List<Object> rows = new ArrayList<Object>();
            for (int i = 0; i < beanList.size(); i++) {
                BaseBean bean = beanList.get(i);
                Map<String, Object> cell = new HashMap<String, Object>();
                cell.put(CELL_ID, bean.getId());
                cell.put(CELL, bean.toJsonArray(from + i));

                rows.add(cell);
            }
            map.put(ROWS_DATA, rows);

            return SUCCESS;
        } catch (BaseException e) {
            map.put("errorMsg", e.getMessage());
            return ERROR;
        }
    }

    protected abstract List<? extends BaseBean> getBeanList() throws BaseException;
    protected abstract long getTotalCount() throws BaseException;
    
    protected SearchCondition getSearchCondition() {
        SearchCondition cond = new SearchCondition();
        cond.setPageNum(pageNum);
        cond.setRowCountPerPage(rowCountPerPage);
        cond.setSortColumn(sortColumn);
        cond.setSortDirection(sortDirection);

        return cond;
    }
}
